package com.ft.oa.service.clueprojectService.Impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ft.oa.domain.clueproject.ClueProjectProgress;
import com.ft.oa.domain.dto.knowledgeBase.AttachmentDto;
import com.ft.oa.domain.dto.sys.SecurityUser;
import com.ft.oa.enums.DirectoryTypeEnum;
import com.ft.oa.mapper.clueprojectMapper.ClueProjectProgressMapper;
import com.ft.oa.mapper.sys.SysUserMapper;
import com.ft.oa.service.clueprojectService.IClueProjectprogressServer;
import com.ft.oa.service.knowledgeBase.DirectoryService;
import com.ft.oa.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Service
public class ClueProjectprogressServerImpl implements IClueProjectprogressServer {
    @Resource
    private ClueProjectProgressMapper clueProjectProgressMapper;
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private DirectoryService directoryService;
    @Override
    public HashMap Clueprogress(String gressId) {
        HashMap<String,Object> gressMap = new HashMap<>();
        QueryWrapper<ClueProjectProgress> wrapper = new QueryWrapper<>();
        wrapper.eq("clue_project_id",gressId);
        List<ClueProjectProgress> clueProjectProgresses = clueProjectProgressMapper.selectList(wrapper);
        clueProjectProgresses.forEach(progress ->{
            progress.setCreatorName(sysUserMapper.selectById(progress.getCreator()).getUsername());
            String achment = progress.getAchment();
            progress.setAchment("");
            progress.setAttachments(JSONObject.parseArray(achment, AttachmentDto.class));
        });
        gressMap.put("progressList",clueProjectProgresses);
        return gressMap;
    }

    @Override
    public int addClueprogress(ClueProjectProgress clueProjectProgress) {
        // 获取当前用户信息
        SecurityUser currentUser = SecurityUtils.getCurrentUser();
        // 获取当前用户名称用作展示售前项目管理
        Long creator = currentUser.getId();
        clueProjectProgress.setCreator(creator);
        List<AttachmentDto> attachments = clueProjectProgress.getAttachments();
        if(attachments != null && attachments.size() > 0){
            String json = JSONArray.toJSONString(attachments);
            clueProjectProgress.setAchment(json);
        }else{
            clueProjectProgress.setAchment(null);
        }
        int insert = clueProjectProgressMapper.insert(clueProjectProgress);
        directoryService.saveProAttachment(attachments, DirectoryTypeEnum.PRO_FD_CLUE.getCode(),String.valueOf(clueProjectProgress.getId()),null,clueProjectProgress.getProjectnumber());
        return insert;
    }

    @Override
    public HashMap<String, Object> ClueprogressId(String id) {
        QueryWrapper<ClueProjectProgress> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        List<ClueProjectProgress> clueProjectProgresses = clueProjectProgressMapper.selectList(wrapper);
        clueProjectProgresses.forEach(e ->{
            String achment = e.getAchment();
            e.setAchment("");
            e.setAttachments(JSONObject.parseArray(achment, AttachmentDto.class));
        });
        HashMap<String,Object> gressesmap = new HashMap<>();
        gressesmap.put("gressesData",clueProjectProgresses);
        return gressesmap;
    }
}
